package
{
	import org.flixel.*;

	public class Dude extends FlxSprite		
	{
		private var bullets:Array;		
		private var bulletIndex:int;		
		
		public function Dude(dudeType:int, cvs:Array)
		{
			var dude:Class = getDudeImg(dudeType);
			super(FlxG.width/2-6, FlxG.height - 65, dude );
			bullets = cvs;	
			bulletIndex = 0;
		}
		
		private function getDudeImg(d:int):Class {
			if(d == 1){
				return Resources.NinjaDude;	
			}
			if(d == 2){
				return Resources.AndroidDude;
			}
			if(d == 3){
				return Resources.TuxDude;	
			}
			if(d == 4){
				return Resources.PeterDude;
			}
			//
			return Resources.PeterDude;
				
		}
		

		override public function update():void
		{
			velocity.x = 0;	
			
			if(FlxG.keys.LEFT) {
				velocity.x -= 150;	
			}
			
			if(FlxG.keys.RIGHT)	{
				velocity.x += 150;
			}
			
			super.update();
		
			if(FlxG.keys.justPressed("SPACE"))
			{
				var b:Cvs = bullets[bulletIndex];	
				b.reset(x + width / 2 - b.width, y -10 );
				b.velocity.y = -540;					
				bulletIndex++;							
				if(bulletIndex >= bullets.length)		
					bulletIndex = 0;					
			}
			
			
			if(x > FlxG.width-width-4)
				x = FlxG.width-width-4; 
			if(x < 4)
				x = 4;	
		}
	}
} 